﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HelloASPX
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                HttpContext.Current.Response.Write("<script>alert('请先登陆！');location='Login.aspx'</script>");
            }
            else
            {
                userLogin.Text = Session["username"].ToString() + "恭喜您成功登录";
            }
            /*if (Session["username"] == null)
            {
                //HttpContext.Current.Response.Write("<script>alert('请先登陆！');location='Login.aspx'</script>");
            }
            else
            {
                //userLogin.Text = Request["username"].ToString() + "恭喜您成功登录";
                userLogin.Text = Session["username"].ToString() + "恭喜您成功登录";
            }
            userLogin.Text = Session["username"].ToString() + "恭喜您成功登录";*/

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            

        }

        protected void txtAnswer_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNode usernode = doc.SelectSingleNode("//user[username='" + DropDownList1.SelectedValue + "']");
            if (usernode != null)
            {
                usernode.SelectSingleNode("username").InnerText = username.Text;
                usernode.SelectSingleNode("e-mail").InnerText = txtEmail.Text;
                usernode.SelectSingleNode("sex").InnerText = sexBList.SelectedValue;
                usernode.SelectSingleNode("age").InnerText = txtAge.Text;
                usernode.SelectSingleNode("birthdate").InnerText = txtcsrq.Text;
                usernode.SelectSingleNode("tel").InnerText = txtTel.Text;
                usernode.SelectSingleNode("passwordQuestion").InnerText = ddlQu.SelectedValue;
                usernode.SelectSingleNode("answer").InnerText = txtAnswer.Text;
                if (FileUpImage.FileName != "")
                {
                    string photopath = "~/img/" + FileUpImage.FileName;
                    usernode.SelectSingleNode("photo").InnerText = photopath;
                }
                doc.Save(Server.MapPath("~/App_Data/user.xml"));
                Response.Write("<script>alert('修改成功！');</script>");
            }
            else
            {
                Response.Write("<script>alert('修改失败！没有找到该用户');</script>");
            }
        }

        protected void select_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNode usernode = doc.SelectSingleNode("//user[username='" + DropDownList1.SelectedValue + "']");
            if (usernode != null)
            {
                Panel1.Visible = true;
                username.Text = usernode.SelectSingleNode("username").InnerText;
                txtEmail.Text = usernode.SelectSingleNode("e-mail").InnerText;
                if (usernode.SelectSingleNode("sex").InnerText == "男")
                {
                    sexBList.SelectedValue = "男";
                }
                else
                {
                    sexBList.SelectedValue = "女";
                }
                txtAge.Text = usernode.SelectSingleNode("age").InnerText;
                txtcsrq.Text = usernode.SelectSingleNode("birthdate").InnerText;
                txtTel.Text = usernode.SelectSingleNode("tel").InnerText;
                ddlQu.SelectedValue = usernode.SelectSingleNode("passwordQuestion").InnerText;
                txtAnswer.Text = usernode.SelectSingleNode("answer").InnerText;
                photo.ImageUrl = usernode.SelectSingleNode("photo").InnerText;

                doc.Save(Server.MapPath("~/App_Data/user.xml"));
            }
            else
            {
                Panel1.Visible = false;
            }
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNode usernode = doc.SelectSingleNode("//user[username='" + DropDownList1.SelectedValue + "']");
            usernode.ParentNode.RemoveChild(usernode);
            try
            {
                doc.Save(Server.MapPath("~/App_Data/user.xml"));
                Response.Write("<script>alert('删除成功！');</script>");
            }
            catch
            {
                Response.Write("<script>alert('删除失败！');</script>");
            }
        }
    }
}